<template>
    <div>
        <div class="handle-box">
            <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="delAllSelection">删除</el-button>
            <el-select v-model="departmentSelect" placeholder="所属部门" class="handle-select mr10" @change="departChange">
                <el-option :label="departItem" :value="departItem" :key="index" v-for="(departItem, index) in allDepartment"></el-option>
            </el-select>
            <el-input v-model="query.name" placeholder="姓名" class="handle-input mr10"></el-input>
            <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
        </div>
        <el-table :data="tableData" border ref="multipleTable" @selection-change="handleSelectionChange" style="width: 100%">
            <el-table-column type="selection" width="55"> </el-table-column>
            <el-table-column fixed prop="staffs_id" label="工作人员编号" width="120"> </el-table-column>
            <el-table-column prop="staffs_name" label="姓名" width="120"> </el-table-column>
            <el-table-column prop="staffs_phone" label="手机号" width="150"> </el-table-column>
            <el-table-column prop="staffs_email" label="邮箱" width="180"> </el-table-column>
            <el-table-column prop="staffs_jobNumber" label="工号" width="100"> </el-table-column>
            <el-table-column prop="staffs_type" label="权限" width="100"> </el-table-column>
            <el-table-column prop="staffs_department" label="所属部门" width="120"> </el-table-column>
            <el-table-column prop='staffs_sex' label="性别" width="100"> </el-table-column>
            <el-table-column prop="staffs_isMarry" label="是否婚配" width="100"> </el-table-column>
            <el-table-column prop="staffs_age" label="年龄" width="100"> </el-table-column>
            <el-table-column prop="staffs_jobAge" label="工龄" width="100"> </el-table-column>
            <el-table-column prop="staffs_salary" label="薪资" width="120"> </el-table-column>
            <el-table-column fixed="right" label="操作" width="100">
                <template slot-scope="scope">
                    <el-button type="text" size="small" @click="checkInfoVisible(scope.row)">查看</el-button>
                    <el-button type="text" size="small" @click="handleClick(scope.row)">编辑</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="pagination">
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page="query.pageIndex"
                :page-size="query.pageSize"
                :total="pageTotal"
                @current-change="handlePageChange"
            ></el-pagination>
        </div>
        <!-- <edit-user
            :editData="editData"
            :centerDialogVisible="centerDialogVisible"
            @parentCenterDialogVisibleChange="parentCenterDialogVisibleChange"
        /> -->
        <staffs-info :editData="editData" :infoVisible="infoVisible" @checkInfoVisible="checkInfoVisible" />
    </div>
</template>

<script>
import { delCustomer, staffsData } from '../../../api/index';
import StaffsInfo from './StaffsInfo';
import UsersInfo from '../userChange/UsersInfo';
export default {
    methods: {
        handleClick(row) {
            console.log(row, 'row');
            this.editData = row;
            this.centerDialogVisible = true;
        },
        getData() {
            staffsData().then((res) => {
                this.tableData = res;
                this.resultData = res;
                const depart = [];
                res.map((item) => {
                    if (depart.indexOf(item.staffs_department) === -1) {
                        depart.push(item.staffs_department);
                    }
                });
                this.allDepartment = depart;
            });
        },
        typeChange(val) {
            this.resultData = this.resultData.filter((item) => {
                return item.customer_type === val;
            });
        },
        departChange(val) {
            this.tableData = this.resultData.filter((item) => {
                return item.staffs_department === val;
            });
        },
        parentCenterDialogVisibleChange() {
            this.centerDialogVisible = !this.centerDialogVisible;
        },
        handleSearch() {
            const newData = [];
            if (this.query.name) {
                this.resultData.filter((item) => {
                    // console.log(item);
                    Object.values(item).map((resItem) => {
                        let handleBool = true;
                        if (handleBool) {
                            if (resItem.indexOf(this.query.name) !== -1) {
                                console.log(item);
                                handleBool = false;
                                newData.push(item);
                            }
                        }
                    });
                });
                this.tableData = newData;
            } else {
                this.tableData = resultData;
            }
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    this.$message.success('删除成功');
                    this.tableData.splice(index, 1);
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            console.log(this.multipleSelection[0].customer_id);
            // const
            delCustomer(this.multipleSelection[0]).then((res) => {
                if (res == 'ok') {
                    this.getData();
                    console.log(res, 'res');
                    this.$message.success('已删除');
                }
            });
            // const length = this.multipleSelection.length;
            // let str = '';
            // this.delList = this.delList.concat(this.multipleSelection);
            // for (let i = 0; i < length; i++) {
            //     str += this.multipleSelection[i].name + ' ';
            // }
            // this.$message.error(`删除了${str}`);
            // this.multipleSelection = [];
        },
        handleEdit(index, row) {
            this.idx = index;
            this.form = row;
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
            this.editVisible = false;
            this.$message.success(`修改第 ${this.idx + 1} 行成功`);
            this.$set(this.tableData, this.idx, this.form);
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        },
        // 查看详情
        checkInfoVisible(item) {
            console.log(item);
            this.editData = item;
            this.infoVisible = !this.infoVisible;
        }
    },
    created() {
        this.getData();
    },

    data() {
        return {
            centerDialogVisible: false,
            tableData: [],
            editData: {},
            query: {
                address: '',
                name: '',
                pageIndex: 1,
                pageSize: 10
            },
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1,
            infoVisible: false,
            allDepartment: [],
            departmentSelect: '',
            resultData: []
        };
    },
    components: {
        // EditUser,
        StaffsInfo
    }
};
</script>

<style scoped>
.el-table .cell,
.el-table--border td:first-child .cell,
.el-table--border th:first-child .cell {
    text-align: center;
}
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>